<?php
namespace app\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * 短信记录
 *
 * Class Sms
 * @package app\models
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $type
 * @property string $msgid
 * @property string $mobile
 * @property string $content
 * @property integer $send_time
 * @property integer $status
 * @property string $remark
 */
class Sms extends ActiveRecord
{
    const TYPE_REG = 1;
    const TYPE_CHANGE_PASSWORD = 2;
    const TYPE_VERIFY = 3;
    
    const STATUS_WAIT = 1;
    const STATUS_PAUSED = 2;
    const STATUS_SEND = 3;
    const STATUS_SEND_FAIL = 4;
    const STATUS_SENT = 5;
    const STATUS_SENT_ERROR = 6;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'type', 'send_time', 'status'], 'integer'],
            [['type', 'mobile', 'content'], 'required'],
            [['msgid', 'mobile'], 'string', 'max'=>32],
            [['content', 'remark'], 'safe'],
        ];
    }

    /**
     * 发送短信验证码
     * @param string $mobile 手机号码
     * @param integer $type app\models\Sms::TYPE_REG 验证码类型
     * @param string $code='' 验证码内容，传入空值时自动生成
     * @return boolean
     */
    public static function sendCode($mobile, $type, $code = '')
    {
        if (empty($code)) {
            // $code = Yii::$app->security->generateRandomString(4);
            $code = Util::randomStr(4, '0123456789');
        }
        switch ($type) {
            case Sms::TYPE_REG:
            default:
                $content = '您的验证码是' . $code;
        }
        if (!Sms::send($type, $mobile, $content)) {
            return false;
        }
        Yii::$app->session->set('sms_code_' . $mobile, json_encode(['type'=>$type, 'code'=>$code, 'time'=>time()]));
        return true;
    }

    /**
     * 验证短信验证码
     * @param string $mobile
     * @param integer $type
     * @param string $code
     * @return boolean
     */
    public static function checkCode($mobile, $type, $code)
    {
        $_code = Yii::$app->session->get('sms_code_' . $mobile);
        if (empty($_code)) {
            // 没有找到此手机号码发送的验证码
            return false;
        }
        $_code = json_decode($_code, true);
        if ($type != $_code['type']) {
            // 类型不符
            return false;
        }
        if (strtoupper($code) != strtoupper($_code['code'])) {
            // 验证码不符
            return false;
        }
        if (time() - 10 * 60 > $_code['time']) {
            // 超时
            return false;
        }
        Yii::$app->session->remove('sms_code_' . $mobile);
        return true;
    }
    
    /**
     * 发送短信
     * @param integer $type
     * @param string $mobile
     * @param string $content
     * @return boolean
     */
    private static function send($type, $mobile, $content)
    {
        $sms = new Sms();
        if (!Yii::$app->user->isGuest) {
            $sms->uid = Yii::$app->user->id;
        }
        $sms->type = $type;
        $sms->mobile = $mobile;
        $sms->content = $content;
        $sms->send_time = time();
        $sms->status = Sms::STATUS_WAIT;
        $sms->remark = '';
        $sms->save();
        return (defined('YII_DEBUG') && YII_DEBUG) ? true : self::send_Impl($sms);
    }
    
    /**
     * XX公司短信接口
     * @param \app\models\Sms $sms
     * @return boolean
     */
    private static function send_Impl($sms)
    {
        // TODO:实际短信接口发送
        return false;
    }
}
